package com.ohos.carbon.slice;

import com.ohos.carbon.ResourceTable;
import com.ohos.carbon.provider.StringsProvider;
import com.ohos.carbon.slice.refresh.IRefresh;
import com.ohos.carbon.slice.refresh.SwipeRefreshLayout;
import com.ohos.carbon.slice.refresh.TextOverComponent;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.Component;
import ohos.agp.components.Image;
import ohos.agp.components.ListContainer;
import ohos.agp.components.Text;
import ohos.agp.components.element.ElementScatter;
import ohos.eventhandler.EventHandler;
import ohos.eventhandler.EventRunner;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

import static com.ohos.carbon.ResourceTable.*;
import static com.ohos.carbon.slice.MainAbilitySlice.pf;

public class PaginationSlice extends AbilitySlice implements Component.ClickedListener {
    private static int num = 1;
    private Image back_button, debug, checkbox;
    private Text text_title;
    private ListContainer listContainer;
    private List<String> element = new ArrayList<>(Arrays.asList("Strawberry " + num, "Apple " + num, "Orange " + num, "Lemon " + num, "Beer " + num, "Lime " + num, "Watermelon " + num, "Blueberry " + num, "Plum " + num));
    private StringsProvider stringsProvider;

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(Layout_ability_pagination);
        initView();
        initRefresh();

        MainAbilitySlice.all = (Map<String, Integer>) pf.getAll();
        if (MainAbilitySlice.all.size() != 0) {
            if (MainAbilitySlice.all.containsKey("Pagination")) {
                for (Map.Entry<String, Integer> entry : MainAbilitySlice.all.entrySet()) {
                    if (entry.getKey().equals("Pagination")) {
                        int integer = entry.getValue();
                        integer++;
                        pf.putInt("Pagination", integer);
                        pf.flushSync();
                    }
                }
            } else {
                pf.putInt("Pagination", 1);
                pf.flushSync();
            }
        } else {
            pf.putInt("Pagination", 1);
            pf.flushSync();
        }
    }

    private void initRefresh() {
        stringsProvider = new StringsProvider(element, getContext());
        listContainer.setItemProvider(stringsProvider);
        SwipeRefreshLayout refresh = (SwipeRefreshLayout) findComponentById(ResourceTable.Id_refresh);
        // 创建下拉刷新头部组件
        TextOverComponent overView = new TextOverComponent(this);
        // 设置下拉刷新的头部组件
        refresh.setRefreshOverComponent(overView);
        refresh.setRefreshListener(new IRefresh.RefreshListener() {
            @Override
            public void onRefresh() {
                new EventHandler(EventRunner.getMainEventRunner()).postTask(new Runnable() {
                    @Override
                    public void run() {
                        // 正在刷新，在这里处理业务逻辑
                        loadNextPage();
                        refresh.refreshFinish();
                    }
                });
            }

            /**
             * 允许下拉刷新
             *
             * @return
             */
            @Override
            public boolean enableRefresh() {
                return true;
            }
        });
    }

    private void initView() {
        listContainer = (ListContainer) findComponentById(Id_list_container);
        back_button = (Image) findComponentById(Id_back_button);
        back_button.setClickedListener(this);
        debug = (Image) findComponentById(Id_image1);
        debug.setClickedListener(this);
        checkbox = (Image) findComponentById(Id_image2);
        checkbox.setClickedListener(this);
        text_title = (Text) findComponentById(Id_title_text);
        text_title.setText("Pagination");
        back_button.setImageElement(ElementScatter.getInstance(getContext()).parse(Graphic_carbon_uparrow));
    }

    @Override
    public void onActive() {
        super.onActive();
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }

    @Override
    public void onClick(Component component) {
        if (component.getId() == Id_back_button) {
            onBackPressed();
            num = 1;
            element.clear();
        } else if (component.getId() == Id_image1) {

        } else if (component.getId() == Id_image2) {

        }
    }

    @Override
    protected void onBackPressed() {
        super.onBackPressed();
        num = 1;
        element.clear();
    }

    private void loadNextPage() {
        num++;
        element.add("Strawberry " + num);
        element.add("Apple " + num);
        element.add("Orange " + num);
        element.add("Lemon " + num);
        element.add("Beer " + num);
        element.add("Lime " + num);
        element.add("Watermelon " + num);
        element.add("Blueberry " + num);
        element.add("Plum " + num);
    }
}
